package io.kiki.sba.registry.server.shared.slot;

import io.kiki.sba.registry.common.model.slot.Slots;
import io.kiki.sba.registry.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DiskSlotsRecorder implements SlotsRecorder {

    private static final Logger logger = LoggerFactory.getLogger(DiskSlotsRecorder.class);

    private volatile Slots lastRecord;

    @Override
    public void record(Slots slots) {
        try {
            if (lastRecord == null || lastRecord.getEpoch() != slots.getEpoch()) {
                String slotStr = JsonUtils.writeValueAsString(slots);
                logger.info("[record] record slot: {}", slotStr);
                this.lastRecord = slots;
            }
        } catch (Throwable e) {
            logger.error("[record]", e);
        }
    }
}
